<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ maleCount, femaleCount } = {}) {
      const total = (maleCount || 0) + (femaleCount || 0)
      const data = [
        { 
          value: maleCount || 0, 
          name: '男性',
          itemStyle: { color: '#409EFF' }
        },
        { 
          value: femaleCount || 0, 
          name: '女性',
          itemStyle: { color: '#FF6B9D' }
        }
      ]

      this.chart.setOption({
        title: [
          {
            text: '性别分布',
            left: 'center',
            top: 20,
            textStyle: {
              fontSize: 16,
              fontWeight: 'normal',
              color: '#666'
            }
          },
          {
            text: `总人数: ${total}`,
            left: 'center',
            top: '50%',
            textStyle: {
              fontSize: 20,
              fontWeight: 'bold',
              color: '#333'
            }
          }
        ],
        tooltip: {
          trigger: 'item',
          formatter: function(params) {
            const percent = total > 0 ? ((params.value / total) * 100).toFixed(1) : 0
            return `${params.name}<br/>人数: ${params.value}<br/>占比: ${percent}%`
          }
        },
        legend: {
          orient: 'horizontal',
          bottom: 30,
          data: ['男性', '女性'],
          textStyle: {
            color: '#666'
          },
          itemWidth: 14,
          itemHeight: 14
        },
        series: [
          {
            name: '性别分布',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '50%'],
            avoidLabelOverlap: false,
            label: {
              show: true,
              position: 'outside',
              formatter: function(params) {
                const percent = total > 0 ? ((params.value / total) * 100).toFixed(1) : 0
                return `${params.name}\n${params.value}人\n${percent}%`
              },
              fontSize: 12,
              color: '#666'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 14,
                fontWeight: 'bold'
              },
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            labelLine: {
              show: true,
              lineStyle: {
                color: '#999'
              }
            },
            data: data,
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDelay: function (idx) {
              return idx * 100;
            }
          }
        ]
      })
    }
  }
}
</script>